package com.kantboot.business.gc.service.impl;

import com.kantboot.business.gc.domain.dto.BusGcSwiperSavaDTO;
import com.kantboot.business.gc.domain.dto.BusGcSwiperSearchDTO;
import com.kantboot.business.gc.domain.entity.BusGcSwiper;
import com.kantboot.business.gc.repository.BusGcSwiperRepository;
import com.kantboot.business.gc.service.IBusGcSwiperService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BusGcSwiperServiceImpl implements IBusGcSwiperService {

    @Resource
    private BusGcSwiperRepository repository;

    @Resource
    private IUserSelfService userSelfService;

    @Override
    public PageResult getSwiper(PageParam<BusGcSwiperSearchDTO> pageParam) {
        //根据sort排序
        return PageResult.of(repository.getBodyData(pageParam.getData(),pageParam.getPageable()));
    }

    @Override
    public Boolean addSwiper(BusGcSwiperSavaDTO swiperSavaDTO) {
        log.info("新增轮播图",swiperSavaDTO);
        if (swiperSavaDTO.getId() != null) {
            //修改
            BusGcSwiper byId = repository.getById(swiperSavaDTO.getId());
            if (byId != null) {
                byId.setGameId(swiperSavaDTO.getGameId())
                        .setSort(swiperSavaDTO.getSort())
                        .setFileIdOfImage(swiperSavaDTO.getFileIdOfImage());
                repository.save(byId);
                return true;
            }
            return false;
        }
        BusGcSwiper save = repository.save(new BusGcSwiper()
                .setGameId(swiperSavaDTO.getGameId())
                .setSort(swiperSavaDTO.getSort())
                .setFileIdOfImage(swiperSavaDTO.getFileIdOfImage()));
        if (save != null) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteSwiper(List<BusGcSwiper> tList) {
        //批量删除
        List<Long> id = tList.stream().map(BusGcSwiper::getId).toList();
        repository.deleteAllById(id);
        return true;
    }

    @Override
    public List<BusGcSwiper> getAllSwiper() {
        return repository.findAll();
    }
}
